"use strict";

const fp = require("fastify-plugin");
const jwt = require("../config/jwt.js");

async function main(fastify, opts) {
  fastify.register(require("fastify-jwt"), {
    secret: jwt.secret,
    decode: {
      complete: true,
    },
    sign: {
      algorithm: jwt.algorithm,
      expiresIn: jwt.expiresIn,
    },
    verify: {
      algorithms: [jwt.algorithm],
    },
  });

  fastify.addHook("onRequest", async (request, reply) => {
    if (request.context.config.isAuth !== false) {
      await request.jwtVerify();
    }
  });
}
module.exports = fp(main);
